package org.dromara.student.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.student.domain.vo.XjStudentTaskAnswerVo;
import org.dromara.student.domain.bo.XjStudentTaskAnswerBo;
import org.dromara.student.service.IXjStudentTaskAnswerService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 学生答案
 *
 * @author Lion Li
 * @date 2024-12-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/student/answer")
public class XjStudentTaskAnswerController extends BaseController {

    private final IXjStudentTaskAnswerService xjStudentTaskAnswerService;

    /**
     * 查询学生答案列表
     */
    @SaCheckPermission("student:answer:list")
    @GetMapping("/list")
    public TableDataInfo<XjStudentTaskAnswerVo> list(XjStudentTaskAnswerBo bo, PageQuery pageQuery) {
        return xjStudentTaskAnswerService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出学生答案列表
     */
    @SaCheckPermission("student:answer:export")
    @Log(title = "学生答案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(XjStudentTaskAnswerBo bo, HttpServletResponse response) {
        List<XjStudentTaskAnswerVo> list = xjStudentTaskAnswerService.queryList(bo);
        ExcelUtil.exportExcel(list, "学生答案", XjStudentTaskAnswerVo.class, response);
    }

    /**
     * 获取学生答案详细信息
     *
     * @param answerId 主键
     */
    @SaCheckPermission("student:answer:query")
    @GetMapping("/{answerId}")
    public R<XjStudentTaskAnswerVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String answerId) {
        return R.ok(xjStudentTaskAnswerService.queryById(answerId));
    }

    /**
     * 新增学生答案
     */
    @SaCheckPermission("student:answer:add")
    @Log(title = "学生答案", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody XjStudentTaskAnswerBo bo) {
        return toAjax(xjStudentTaskAnswerService.insertByBo(bo));
    }

    /**
     * 修改学生答案
     */
    @SaCheckPermission("student:answer:edit")
    @Log(title = "学生答案", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody XjStudentTaskAnswerBo bo) {
        return toAjax(xjStudentTaskAnswerService.updateByBo(bo));
    }

    /**
     * 删除学生答案
     *
     * @param answerIds 主键串
     */
    @SaCheckPermission("student:answer:remove")
    @Log(title = "学生答案", businessType = BusinessType.DELETE)
    @DeleteMapping("/{answerIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] answerIds) {
        return toAjax(xjStudentTaskAnswerService.deleteWithValidByIds(List.of(answerIds), true));
    }
}
